package jst.householdmanager.activities;

import java.util.ArrayList;

import jst.householdmanager.R;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListEntryMultiline {

	private Activity activity;

	public ListEntryMultiline(Activity activity) {
		super();
		this.activity = activity;
	}

	public EntryRecord Record(int id, String mainText) {
		return new EntryRecord(id, mainText, null);
	}

	public EntryRecord Record(int id, String mainText, String subText) {
		return new EntryRecord(id, mainText, subText);
	}

	public EntryAdapter Adapter(Context context, int textViewResourceId, ArrayList<EntryRecord> listEntries) {
		return new EntryAdapter(context, textViewResourceId, listEntries);
	}

	public class EntryRecord {
		public int id;
		public String mainText;
		public String subText = null;

		public EntryRecord(int id, String mainText, String subText) {
			super();
			this.id = id;
			this.mainText = mainText;
			this.subText = subText;
		}
	}

	private class EntryAdapter extends ArrayAdapter<EntryRecord> {
		private ArrayList<EntryRecord> listEntries;

		public EntryAdapter(Context context, int textViewResourceId, ArrayList<EntryRecord> listEntries) {
			super(context, textViewResourceId, listEntries);
			this.listEntries = listEntries;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) ListEntryMultiline.this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.listentry_multiline, null);
			}

			final EntryRecord listEntry = this.listEntries.get(position);
			if (listEntry != null) {
				TextView maintxt = (TextView) v.findViewById(R.id.listentry_multiline_maintxt);
				TextView subtxt = (TextView) v.findViewById(R.id.listentry_multiline_subtxt);
				maintxt.setText(listEntry.mainText);

				if (listEntry.subText != null) {
					subtxt.setVisibility(0);
					subtxt.setText(listEntry.subText);
				}

			}
			return v;
		}
	}
}